import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state:{
        //定义共享数据
        count:2,
        user:{
            name:'admin',
            pass:'12345'
        },
        info:[
            {id:1, name:'潇潇1',age:20},
            {id:2, name:'潇潇2',age:41},
            {id:3, name:'潇潇3',age:18},
            {id:4, name:'潇潇4',age:33},
            {id:5, name:'潇潇5',age:14}
          ]
    },
    getters: {
        filterInfo(state){
            //过滤大于20的
            return state.info.filter(info => info.age>20);
        },
        getInfoLength(state,getter){
            return getter.filterInfo.length
        }
    },
    //同步
    mutations:{
        add(state){
            state.count++;
        },
        myadd(state,num){
            state.count += num;
        }
    },
    //异步
    actions:{
        //
        asyncAdd(context){
            // console.log(context);
            setTimeout(()=>{
                context.commit("add");
            },2000);
        }
    },
    modules:{

    }
})